﻿using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.BandedGrid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Main2 : Form
    {
        string user = StaticClass.User; //取出全局变量中的值
        string BatchNo,longUp,longDown,highUp,highDown,thinknessUp,thinknessDown;
        public string connectionString = ConfigurationManager.AppSettings["SqlServerCon"].ToString();
        string MDBstring = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " + Application.StartupPath + "\\QCRecords.MDB;Jet OLEDB:Database Password=Sa123456";
        int CurSelectQCItem; //检测是否点击了类别
        string CurQCItem;//产品类型ID
        double CurQCItemUp = 0.0000;
        double CurQCItemLo = 0.0000;
        int CheckFlag;
        MDBHelper MDBHelpr_ = new MDBHelper();
        GridControl ExportGC;
        
        public Main2()
        {
            InitializeComponent();
            groupControl3.Enabled = true;
        }

        private void initQuery(String itemid)
        {
            //OleDbConnection conn = new OleDbConnection(MDBstring);
            //OleDbCommand comm;
            //string sql = "select LengUp,LengDown,HighUp,HighDown,QXUp,QXDown from ValueSet where ItemId = @itemid";
            //comm = new OleDbCommand(sql, conn);
            //comm.Parameters.AddWithValue("@itemid", itemid);
            //conn.Open();
            //OleDbDataReader dr = comm.ExecuteReader();
            //if (dr.Read())
            //{
            //    txt_longUp.Text = (string)dr["LengUp"];
            //    txt_longDown.Text = (string)dr["LengDown"];
            //    txt_highUp.Text = (string)dr["HighUp"];
            //    txt_highDown.Text = (string)dr["HighDown"];
            //    txt_thinknessUp.Text = (string)dr["QXUp"];
            //    txt_thinknessDown.Text = (string)dr["QXDown"];
            //}
            //comm.Dispose();
            //conn.Close();
        }

        /**
         *    查询物料编号和产品编号   
         */
        public DataTable BatchInfo(string BatchId)
        {
            OleDbConnection FlashConn;
            DataTable dt;
            OleDbDataAdapter da;
            FlashConn = new OleDbConnection(MDBstring);
            FlashConn.Open();
            //创建一个空表
            dt = new DataTable();
            StringBuilder sb = new StringBuilder();
            //   sb.Append("select * from BatchList batch");
            //,valset.Size
            //        ,valset.LengthUp ,valset.LengDown,valset.HighUp,valset.HighDown,
            //        valset.QXUp,valset.QXDwon
            sb.Append(string.Format(@"select BatchID ,ItemID,ProID from BatchList where  BatchId ='" + BatchId + "'"));

            da = new OleDbDataAdapter(sb.ToString(), FlashConn);
            da.Fill(dt);

            return dt;
        }



        private void Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            MenuForm f2 = new MenuForm();
            f2.Show();
            this.Hide();
        }


        #region 事件

        //用户加载窗体时的事件
        private void Main_Load(object sender, EventArgs e)
        {

        }

        //锁定按钮事件
        private void btn_lock_Click(object sender, EventArgs e)
        {
            if (txt_PiHao.Text.Trim() == "")
            {
                MessageBox.Show("无法锁定，请确认批号有值。");
                return;
            }
            //DataTable dt = BatchInfo(txt_PiHao.Text);
            //if (Convert.ToInt32(dt.Rows.Count) == 0)
            //{
            //    MessageBox.Show("无法锁定，请确认是否刷新批号或者批号输入错误");
            //    return;
            //}
            ////获取第一行第二条的字段值
            //string ItemId = dt.Rows[0][1].ToString();
            //this.txt_materialNum.Text = ItemId;
            ////获取第一行第三条的字段值
            //this.txt_productNum.Text = dt.Rows[0][2].ToString();

            this.txt_materialNum.Visible = true;
            this.txt_productNum.Visible = true;
            //initQuery(ItemId);
            btn_lock.Enabled = false;
            txt_PiHao.Enabled = false;
            groupControl3.Enabled = true;

            loadMasterGrid2();
        }

        //解除锁定按钮事件
        private void btn_unlock_Click(object sender, EventArgs e)
        {
            btn_lock.Enabled = true;
            txt_PiHao.Enabled = true;
            groupControl3.Enabled = false;

            txt_thinknessUp.Text = "";
            txt_thinknessDown.Text = "";
            this.txt_materialNum.Text = "";
            this.txt_productNum.Text = "";
        }
        //确定按钮事件 回车事件
        private void btn_确定_Click(object sender, EventArgs e)
        {
            btn_确定_KeyDown(null, new KeyEventArgs(Keys.Enter));
        }
        private void btn_确定_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && CurSelectQCItem == 0)
            {
                MessageBox.Show("请选择一种检测类别");
            }
            if (e.KeyCode == Keys.Enter && CurSelectQCItem > 0)
            {
                if (txt_当前序号.Text != "1")
                {
                    int tablecount = getcount2();
                    int resultcount = getcount();
                    if (tablecount >= resultcount)
                    {
                        MessageBox.Show("当前测量序号的数量不能查过序号1的数量");
                        FlashRec();
                        return;
                    }
                }

                string val = txt_检测值.Text.Trim();
                if (val == "")
                {
                    MessageBox.Show("请输入检测值");
                    return;
                }

                if (InsRec(val))
                {
                    //if (txt_当前序号.Text == "1")
                    //{
                    //    if (CurSelectQCItem == 2)
                    //    {
                    //        countlen++;
                    //    }
                    //    else if (CurSelectQCItem == 3)
                    //    {
                    //        countwid++;
                    //    }
                    //    else if (CurSelectQCItem == 4)
                    //    {
                    //        counthei++;
                    //    }
                    //}
                }
                else
                {
                    MessageBox.Show("登记异常");
                }

                txt_检测值.Text = "";
                txt_检测值.Focus();
                FlashRec();

                loadMasterGrid2();
            }
            else
            {
                //txt_值录入.Text = "0";
            }

        }

        //长度框点击事件
        private void gc_长度_Click(object sender, EventArgs e)
        {
            selectItemColor(1);
            txt_检测值.Focus();
            FlashRec();
            //if (this.txt_longUp.Text == "" || this.txt_longDown.Text == "")
            //{
            //    MessageBox.Show("无上下限，无法选中");
            //}
            //else
            //{
            //    selectItemColor(1);
            //    txt_检测值.Focus();
            //    FlashRec();
            //}

        }

        //高度框点击事件
        private void gc_高度_Click(object sender, EventArgs e)
        {
            selectItemColor(2);
            txt_检测值.Focus();
            FlashRec();
            //if (this.txt_highUp.Text == "" || this.txt_highDown.Text == "")
            //{
            //    MessageBox.Show("无上下限，无法选中");
            //}
            //else
            //{
            //    selectItemColor(2);
            //    txt_检测值.Focus();
            //    FlashRec();
            //}

        }

        //厚度框点击事件
        private void gc_厚度_Click(object sender, EventArgs e)
        {
            selectItemColor(3);
            txt_检测值.Focus();
            FlashRec();
            //if (this.txt_thinknessUp.Text == "" || this.txt_thinknessDown.Text == "")
            //{
            //    MessageBox.Show("无上下限，无法选中");
            //}
            //else
            //{
            //    selectItemColor(3);
            //    txt_检测值.Focus();
            //    FlashRec();
            //}

        }

        //gridview item点击事件
        private void gridCon_view_Click(object sender, EventArgs e)
        {
            try
            {
                DataRow dr = this.gridView1.GetDataRow(this.gridView1.FocusedRowHandle);//获取选中行
                string str = gridView1.GetFocusedRowCellValue("序号").ToString();//获取某一行的指定列的值
                this.LbLineNum.Text = str;
            }
            catch
            {
                MessageBox.Show("操作异常！");
            }
        }

        //删除按钮点击事件
        private void btn_删除_Click(object sender, EventArgs e)
        {
            if (LbLineNum.Text.Trim() == "") {
                return;
            }
               
            string tmpstr = "确定删除？";
            if (MessageBox.Show(tmpstr, "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                try
                {
                    DelRec(Convert.ToInt32(LbLineNum.Text.Trim()));
                    loadMasterGrid2();
                }
                catch
                {
                    MessageBox.Show("删除失败");
                }

            }
            txt_检测值.Focus();
        }

        private void btn_序号减_Click(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(txt_当前序号.Text);
            if (i == 1)
            {
                MessageBox.Show("序号不能少于1");
            }
            else {
                int show = i - 1;
                txt_当前序号.Text = Convert.ToString(show);
            }

        }

        private void btn_序号加_Click(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(txt_当前序号.Text);
            int show = i + 1;
            txt_当前序号.Text = Convert.ToString(show);
        }

        #endregion

        /// <summary>
        /// 选择 长宽厚 ，变色
        /// </summary>
        /// <param name="num"></param>
        public void selectItemColor(int num = 0){
            CurSelectQCItem = num;


            txt_thinknessUp.BackColor = (num == 3 ? Color.LightGreen : Color.White);
            txt_thinknessDown.BackColor = (num == 3 ? Color.LightGreen : Color.White);

            string tmpstr;
            tmpstr =
                    (num == 3 ? txt_thinknessUp.Text.Trim() : "");

            CurQCItemUp = tmpstr == "" ? 0 : Convert.ToDouble(tmpstr);
            tmpstr =(num == 3 ? txt_thinknessDown.Text.Trim() : "");

            CurQCItemLo = tmpstr == "" ? 0 : Convert.ToDouble(tmpstr);

        }


        /// <summary>
        /// 值录入到图例中，并且插入数据到数据库内
        /// </summary>
        /// <param name="_qcvalue"></param>
        /// <returns></returns>
        public bool InsRec(string _qcvalue)
        {
            bool ret = false;
            string syncMDBsql;
            BatchNo = txt_PiHao.Text.Trim();//产品编号
            CurQCItem = CurSelectQCItem.ToString();//产品类型ID 长1 宽2 厚3

            syncMDBsql = "insert into MicrometerData([BatchNo],[QCItemId],[QCValue],[QCValueUp],[QCValueLo],[UpdNo],[OrderNo],[DelFlag]) "
                        + "values ('" + txt_PiHao.Text + "','" + CurQCItem + "','"  + _qcvalue + "','" + CurQCItemUp.ToString() + "','" + CurQCItemLo.ToString() + "','" + user + "','"
                        + txt_当前序号.Text + "','" + (CheckFlag == 1 ? "1" : "0")+"')"; 
            try
            {
                SqlConnection syncMDBConn = new SqlConnection(connectionString);
                syncMDBConn.Open();
                SqlCommand syncMDBComm = new SqlCommand();
                syncMDBComm.Connection = syncMDBConn;
                syncMDBComm.CommandText = syncMDBsql;
                syncMDBComm.ExecuteNonQuery();
                syncMDBComm.Dispose();
                syncMDBConn.Close();
                ret = true;
            }
            catch
            {
                MessageBox.Show("输入有误，数据异常");
            }
            return ret;
        }

        /// <summary>
        /// ---------检测值-------------  gridview呈现方法
        /// </summary>1111
        /// <returns></returns>
        public bool FlashRec()
        {
            bool ret = false;
            //QCRecordsGrid.DefaultCellStyle = new DataGridViewCellStyle();//在未设置其他单元格样式属性的情况下，获取或设置应用于 DataGridView 中的单元格的默认单元格样式。 
            gridCon_view.DataSource = null;
            string tmpsql, wherestr;
            SqlConnection FlashConn = new SqlConnection(connectionString);
            FlashConn.Open();
            DataTable dt = new DataTable();
            CurQCItem = CurSelectQCItem.ToString();
            wherestr = " where DelFlag = 0 and batchno='" + txt_PiHao.Text + "' and qcitemid='" + CurQCItem + "' and OrderNo='" + txt_当前序号.Text + "'order by id desc";
            tmpsql = "select qcvalue as 检测值,id as 序号,qcvalueUp as 上限 ,qcvalueLo as 下限 from MicrometerData " + wherestr;
            SqlDataAdapter da = new SqlDataAdapter(tmpsql, FlashConn);
            da.Fill(dt);
            gridCon_view.DataSource = dt;

            //if (dt.Rows.Count > 0)
            //{
            //    DataRow row = gridView3.GetDataRow(0);
            //    this.LbLineNum.Text = row["序号"].ToString();
            //}
            //if (CurSelectQCItem == 2)
            //{
            //    countlen = dt.Rows.Count;
            //}
            //else if (CurSelectQCItem == 3)
            //{
            //    countwid = dt.Rows.Count;
            //}
            //else if (CurSelectQCItem == 4)
            //{
            //    counthei = dt.Rows.Count;
            //}
            ret = true;
            FlashConn.Close();
            //ChangeColor();
            return ret;
        }

        /// <summary>
        /// 删除检测值一行
        /// </summary>
        /// <param name="_qclinenum"></param>
        /// <returns></returns>
        public bool DelRec(int _qclinenum)
        {
            bool ret = false;

            try
            {
                SqlConnection ConnDel = new SqlConnection(connectionString);
                ConnDel.Open();
                SqlCommand CmdDel = new SqlCommand();
                CmdDel.CommandText = "Update MicrometerData set DelFlag=1 where id=" + _qclinenum.ToString();
                CmdDel.Connection = ConnDel;
                CmdDel.ExecuteNonQuery();
                CmdDel.Dispose();
                ConnDel.Close();
                FlashRec();
            }
            catch
            {
            }

            return ret;
        }

        public Int32 getcount()
        {
            gridCon_view.DataSource = null;
 
            SqlConnection FlashConn = new SqlConnection(connectionString);
            FlashConn.Open();
            DataTable dt = new DataTable();

            CurQCItem = CurSelectQCItem.ToString();
            string wherestr = " where DelFlag = 0 and batchno='" + txt_PiHao.Text + "' and qcitemid='" + CurQCItem + "' and OrderNo= '" + 1 + "'";
            string tmpsql = "select count(*) from MicrometerData " + wherestr;
            SqlDataAdapter da = new SqlDataAdapter(tmpsql, FlashConn);

            da.Fill(dt);
            return Convert.ToInt32(dt.Rows[0][0]);
        }

        public Int32 getcount2()
        {
            gridCon_view.DataSource = null;

            SqlConnection FlashConn = new SqlConnection(connectionString);
            FlashConn.Open();
            DataTable dt = new DataTable();

            CurQCItem = CurSelectQCItem.ToString();
            string wherestr = " where DelFlag = 0 and batchno='" + txt_PiHao.Text + "' and qcitemid='" + CurQCItem + "' and OrderNo= '" + this.txt_当前序号.Text + "'";
            string tmpsql = "select count(*) from MicrometerData " + wherestr;
            SqlDataAdapter da = new SqlDataAdapter(tmpsql, FlashConn);
            da.Fill(dt);
            return Convert.ToInt32(dt.Rows[0][0]);
        }


        /// <summary>
        /// 创建DEV bandedgridview 动态表格
        /// </summary>
        /// 
        public void loadMasterGrid2()
        {
            this.groupCon_图例.Controls.Clear();//清空图例gridview
            DataTable dt = MDBHelpr_.getMaster(connectionString, txt_PiHao.Text);//获取图例
            //获取行 ，按创建时间排序
            //DataRow[] countrow = dt.Select("QCItemId=1", " CreatedDatetime ");
            //DataRow[] countrow1 = dt.Select("QCItemId=2", "CreatedDatetime");
            DataRow[] countrow2 = dt.Select("QCItemId=3", "CreatedDatetime");

            //HeaderUnitView dgv = new HeaderUnitView();
            AdvBandedGridView ABGV = new DevExpress.XtraGrid.Views.BandedGrid.AdvBandedGridView();
            // ABGV.Appearance.
            ABGV.Appearance.Row.Font = new System.Drawing.Font("Tahoma", 13F);
            GridControl GC = new GridControl();
            GC.MainView = ABGV;
            ABGV.GridControl = GC;
            GC.Name = "GCID";
            GC.Dock = System.Windows.Forms.DockStyle.Fill;
            ABGV.GroupPanelText = "图例";
            ABGV.OptionsBehavior.Editable = false;
            ABGV.OptionsView.ColumnAutoWidth = false;
            //  ABGV.OptionsBehavior.Editable = false;
            ABGV.OptionsSelection.EnableAppearanceFocusedRow = false;
            DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn BGC = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();
            BGC.FieldName = "OrderNo";
            BGC.Caption = "序号";
            //BGC.Name = "OrderNo";
            BGC.Name = "OrderNo";
            BGC.Visible = true;
            BGC.VisibleIndex = 0;
            ABGV.Columns.Add(BGC);
            //    ABGV.columnh


            DevExpress.XtraGrid.Views.BandedGrid.GridBand GB = new DevExpress.XtraGrid.Views.BandedGrid.GridBand();
            GB.Caption = "  ";
            GB.Name = "GB";
            GB.VisibleIndex = 0;
            // GB.Width = 225;
            GB.Columns.Add(BGC);

            //// GB.
            //DevExpress.XtraGrid.Views.BandedGrid.GridBand GB2 = new DevExpress.XtraGrid.Views.BandedGrid.GridBand();
            //GB2.Caption = "长度（mm）";
            //GB2.Name = "GB2";
            //GB2.VisibleIndex = 1;
            ////  GB2.Width = 225;
            //GB2.AppearanceHeader.BackColor = Color.LightGreen;

            //DevExpress.XtraGrid.Views.BandedGrid.GridBand GB3 = new DevExpress.XtraGrid.Views.BandedGrid.GridBand();
            //GB3.Caption = "高度（mm）";
            //GB3.Name = "GB3";
            //GB3.VisibleIndex = 2;
            // GB3.Width = 225;
            //  GB3.AppearanceHeader.ForeColor = Color.Red;

            DevExpress.XtraGrid.Views.BandedGrid.GridBand GB4 = new DevExpress.XtraGrid.Views.BandedGrid.GridBand();
            GB4.Caption = "厚度（mm）";
            GB4.Name = "GB4";
            GB4.VisibleIndex = 3;
            //  GB4.Width = 225;

            //添加第一行  第一个为空， 剩下分别为长宽厚
            ABGV.Bands.AddRange(new DevExpress.XtraGrid.Views.BandedGrid.GridBand[] {
            GB,
            //GB2,
            //GB3,
            GB4});

            //  ABGV.Columns[0].AppearanceHeader.BackColor = Color.Yellow;
            //DataGridViewTextBoxColumn tcMC = new DataGridViewTextBoxColumn();
            //tcMC.HeaderText = "序号";
            //tcMC.Name = "OrderNo";
            //tcMC.ReadOnly = true;
            //dgv.Columns.Add(tcMC);

            // int tempint = -1;
            DataTable ndt = new DataTable();
            ndt.Columns.Add("OrderNo", typeof(string));
            //IList<BandedGridColumn> list1 = new List<BandedGridColumn>();
            //IList<BandedGridColumn> list2 = new List<BandedGridColumn>();
            IList<BandedGridColumn> list3 = new List<BandedGridColumn>();

            //for (int i = 0; i < countrow.Length; i++)
            //{
            //    BandedGridColumn tcNan = new BandedGridColumn();

            //    int temp = i;
            //    temp++;
            //    tcNan.FieldName = "L" + temp;
            //    tcNan.Name = tcNan.FieldName;
            //    tcNan.Visible = true;
            //    tcNan.Width = 60;
            //    list1.Add(tcNan);
            //    ndt.Columns.Add("L" + temp, typeof(string));
            //}

            //for (int i = 0; i < countrow1.Length; i++)
            //{
            //    BandedGridColumn tcNan = new BandedGridColumn();

            //    int temp = i;
            //    temp++;
            //    tcNan.FieldName = "W" + temp;
            //    tcNan.Name = tcNan.FieldName;
            //    tcNan.AutoFillDown = true;
            //    tcNan.Visible = true;
            //    list2.Add(tcNan);
            //    //GB3.Columns.Add(tcNan);
            //    ndt.Columns.Add("W" + temp, typeof(string));
            //}

            for (int i = 0; i < countrow2.Length; i++)
            {
                BandedGridColumn tcNan = new BandedGridColumn();


                int temp = i;
                temp++;
                tcNan.FieldName = "H" + temp;
                tcNan.Name = tcNan.FieldName;
                tcNan.Visible = true;
                list3.Add(tcNan);
                //GB4.Columns.Add(tcNan);
                ndt.Columns.Add("H" + temp, typeof(string));

            }

            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
                //DevExpress.XtraGrid.Columns.GridColumn;


                //tcNan.ReadOnly = true;
                //dgv.Columns.Add(tcNan);

                //ABGV.Columns.Add(tcNan);
                //tcNan.VisibleIndex = i + 1;
            //}
            //for (int i = list1.Count - 1; i >= 0; i--)
            //{
            //    GB2.Columns.Add(list1[i]);
            //    GB2.Columns[0].AppearanceCell.ForeColor = Color.Blue;

            //    //  GB2.Columns[0].AppearanceHeader.BackColor = Color.Red;
            //}
            //for (int i = list2.Count - 1; i >= 0; i--)
            //{
            //    GB3.Columns.Add(list2[i]);
            //    GB3.Columns[0].AppearanceCell.ForeColor = Color.Orange;
            //}
            for (int i = list3.Count - 1; i >= 0; i--)
            {
                GB4.Columns.Add(list3[i]);
                GB4.Columns[0].AppearanceCell.ForeColor = Color.DeepSkyBlue;
            }

            // GC.MainView.EndSelection();
            groupCon_图例.Controls.Add(GC);


            DataTable dt2 = MDBHelpr_.getMasterData(connectionString, txt_PiHao.Text);
            DataTable test = MDBHelpr_.getorder(connectionString, txt_PiHao.Text);

            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                DataRow[] drs = ndt.Select("OrderNo = '" + dt2.Rows[i]["OrderNo"].ToString() + "'");
                if (drs.Length == 0)
                {
                    DataRow ndr = ndt.NewRow();
                    ndr["OrderNo"] = dt2.Rows[i]["OrderNo"].ToString();
                    ndt.Rows.Add(ndr);
                }

            }

            //for (int g = 0; g < test.Rows.Count; g++)
            //{
            //    DataRow[] ncountrow = dt2.Select("QCItemId=2 and OrderNo='"+test.Rows[g][0]+"'", " CreatedDatetime ");
            //    DataRow[] ncountrow1 = dt2.Select("QCItemId=3", " CreatedDatetime ");
            //    DataRow[] ncountrow2 = dt2.Select("QCItemId=4", " CreatedDatetime ");


            //}
            //DataRow[] ncountrow = dt2.Select("QCItemId=1", "OrdernO, CreatedDatetime ");
            //DataRow[] ncountrow1 = dt2.Select("QCItemId=2", " OrdernO,CreatedDatetime ");
            //DataRow[] ncountrow2 = dt2.Select("QCItemId=3", " OrdernO,CreatedDatetime ");
            //foreach (DataRow dr in ndt.Rows)
            //{
            //    string odn = dr["OrderNo"].ToString();
            //    DataRow[] ncountrow = dt2.Select("QCItemId=1 and OrderNo = '" + odn + "'", "OrdernO, CreatedDatetime ");
            //    for (int i = 0; i < ncountrow.Length; i++)
            //    {
            //        dr["L" + (i + 1)] = ncountrow[i]["QCValue"].ToString();

            //    }
            //}
            //foreach (DataRow dr in ndt.Rows)
            //{
            //    string odn = dr["OrderNo"].ToString();
            //    DataRow[] ncountrow = dt2.Select("QCItemId=2 and OrderNo = '" + odn + "'", "OrdernO, CreatedDatetime ");
            //    for (int i = 0; i < ncountrow.Length; i++)
            //    {
            //        dr["W" + (i + 1)] = ncountrow[i]["QCValue"].ToString();

            //    }
            //}
            foreach (DataRow dr in ndt.Rows)
            {
                string odn = dr["OrderNo"].ToString();
                DataRow[] ncountrow = dt2.Select("QCItemId=3 and OrderNo = '" + odn + "'", "OrdernO, CreatedDatetime ");
                for (int i = 0; i < ncountrow.Length; i++)
                {
                    dr["H" + (i + 1)] = ncountrow[i]["QCValue"].ToString();

                }
            }
            //for (int i = 0; i < ncountrow.Length; i++)
            //{
            //    string odn = ncountrow[i]["OrderNo"].ToString();
            //    DataRow[] drs = ndt.Select("OrderNo = '" + odn + "'");

            //    temp++;
            //    drs[0]["L" + temp] = ncountrow[i]["QCValue"].ToString();

            //}
            //for (int i = 0; i < ncountrow1.Length; i++)
            //{
            //    string odn = ncountrow1[i]["OrderNo"].ToString();
            //    DataRow[] drs = ndt.Select("OrderNo = '" + odn + "'");
            //    int temp = i % countrow1.Length;
            //    drs[0]["W" + (temp + 1)] = ncountrow1[i]["QCValue"].ToString();

            //}
            //for (int i = 0; i < ncountrow2.Length; i++)
            //{
            //    string odn = ncountrow2[i]["OrderNo"].ToString();
            //    DataRow[] drs = ndt.Select("OrderNo = '" + odn + "'");
            //    int temp = i % countrow2.Length;
            //    drs[0]["H" + (temp + 1)] = ncountrow2[i]["QCValue"].ToString();
            //}

            GC.DataSource = ndt;
            ExportGC = GC;
            //gridview 剩余部分背景颜色
            ABGV.Appearance.Empty.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(236)))), ((int)(((byte)(239)))));
            ABGV.Appearance.Empty.Options.UseBackColor = true;
            ABGV.OptionsView.ShowGroupPanel = false;
            ABGV.BestFitColumns();
            //ABGV.OptionsView.ColumnAutoWidth = true;
            //  ABGV.Columns[1].AppearanceHeader.ForeColor = Color.Gold;

            //dgv.ColumnTreeView = new TreeView[] { tv };
            ////设置其他属性
            //dgv.AutoGenerateColumns = false;                                    //不自动增加列
            //dgv.RowHeadersVisible = false;                                      //行头不可见
            //dgv.AllowUserToAddRows = false;
            //dgv.RowTemplate.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            //dgv.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            //dgv.ColumnDeep = 2;
            //dgv.CellHeight = 25;
            //dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            //dgv.ColumnHeadersHeight = 50;
            //dgv.Dock = DockStyle.Fill;
            //dgv.RefreshAtHscroll = true;
            //// dgv.Cell
            //dgv = loaddata(dgv);
            //this.groupBox6.Controls.Add(dgv);

        }
    }
}
